package com.selly.hanjuang;

import java.util.ArrayList;
import java.util.List;
import com.selly.hanjuang.lib.MyApplication;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.selly.hanjuang.lib.ImageLoader;
import com.selly.hanjuang.lib.JSONParser;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class CariBuktiPengirimanActivity extends Activity {	
	
	public ImageLoader imageLoader;
	{

		imageLoader = new ImageLoader(null);
	}

	TextView txtIdPemesanan;
	TextView txtInfo;
	ImageButton btnSearch;
	/*
	TextView txtLinkImageProduk;
	TextView txtKomposisiProduk;
	TextView txtHargaProduk;
	TextView txtKemasanProduk;
	TextView txtBeratProduk;
	*/
	
	// TextView txtWebsite;
	// ImageButton btnAddToCart;

	String id_pemesanan;

	// Progress Dialog
	private ProgressDialog pDialog;

	// JSON parser class
	JSONParser jsonParser = new JSONParser();

	// url detail produk
	//
	//private static final String url_bukti_pengiriman = "http://10.0.2.2/hanjuang/json/get_bukti_pengiriman.php";
	private static final String url_bukti_pengiriman = "http://hanjuang.web.id/json/get_bukti_pengiriman.php";
	// JSON Node names
	private static final String TAG_SUCCESS = "success";
	private static final String TAG_MESSAGE = "message";
	private static final String TAG_BUKTI_KIRIM = "bukti_pengiriman";
	
	private static final String TAG_FOTO = "foto";
	
	// JSON Node names
	//private static final String TAG_SUCCESS_INSERT = "success";

	// private static final String TAG_WEBSITE_RS = "website_rs";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cari_bukti_pengiriman);

		// detail produk from intent
		//Intent i = getIntent();
		// getting id_produk from intent
		//id_produk = i.getStringExtra(TAG_ID_PRODUK);
		txtIdPemesanan = (TextView) findViewById(R.id.txtIdPemesanan);
		txtInfo = (TextView) findViewById(R.id.txtInfo);
		id_pemesanan = txtIdPemesanan.getText().toString();
		
		btnSearch = (ImageButton) findViewById(R.id.btnSearchBukti);
		
		btnSearch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new GetBuktiPengirimanActivity().execute();
			}
		});		
	}

	/**
	 * Background Async Task to Get complete detail produk
	 * */
	class GetBuktiPengirimanActivity extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(CariBuktiPengirimanActivity.this);
			pDialog.setMessage("Mohon Tunggu :) !");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		/**
		 * Getting detail produk in background thread
		 * */
		protected String doInBackground(String... params) {

			// updating UI from Background Thread
			runOnUiThread(new Runnable() {
				public void run() {

					// Check for success tag
					int success;
					try {
						id_pemesanan = txtIdPemesanan.getText().toString();
						//id_pemesanan = "2";
						// Building Parameters
						List<NameValuePair> params = new ArrayList<NameValuePair>();
						params.add(new BasicNameValuePair("id_pemesanan",
								id_pemesanan));

						// getting detail produk by making HTTP request
						JSONObject json = jsonParser.makeHttpRequest(
								url_bukti_pengiriman, "GET", params);

						// check your log for json response
						Log.d("Detail Produk", json.toString());

						// json success tag
						success = json.getInt(TAG_SUCCESS);
						if (success == 1) {
							// successfully received detail produk
							JSONArray BuktiKirimObj = json.getJSONArray(TAG_BUKTI_KIRIM);

							// get detail produk object from JSON Array
							JSONObject buktiKirim = BuktiKirimObj.getJSONObject(0);

							// detail produk yang sesuai dengan id_produk						
							/*
							txtLinkImageProduk = (TextView) findViewById(R.id.link_image_produk);
							txtKomposisiProduk = (TextView) findViewById(R.id.detail_komposisi_produk);
							txtHargaProduk = (TextView) findViewById(R.id.detail_harga_produk);
							txtKemasanProduk = (TextView) findViewById(R.id.detail_kemasan_produk);
							txtBeratProduk = (TextView) findViewById(R.id.detail_berat_produk);
							*/
							// txtWebsite = (TextView)
							// findViewById(R.id.website_rs);
 
							//ImageView thumb_image = (ImageView) findViewById(R.id.detail_list_image);
							ImageView thumb_image = (ImageView) findViewById(R.id.imgBuktiPengiriman);

							// tampilkan detail produk
							/*
							txtNamaProduk.setText(produk
									.getString(TAG_NAMA_PRODUK));
							final String sNamaProduk = produk
									.getString(TAG_NAMA_PRODUK);
							
							txtLinkImageProduk.setText(produk
									.getString(TAG_LINK_IMAGE_PRODUK));
							final String sLinkImage = produk
									.getString(TAG_LINK_IMAGE_PRODUK);
							
							txtKomposisiProduk.setText(produk
									.getString(TAG_KOMPOSISI_PRODUK));
							txtHargaProduk.setText("Rp "
									+ produk.getString(TAG_HARGA_PRODUK));
							final String sHarga = produk.getString(TAG_HARGA_PRODUK);
							
							txtKemasanProduk.setText(produk
									.getString(TAG_KEMASAN_PRODUK));
							txtBeratProduk.setText(produk
									.getString(TAG_BERAT_PRODUK));
							// txtWebsite.setText(rumah_sakit
							// .getString(TAG_WEBSITE_RS));
 							*/
							
							imageLoader.DisplayImage(
									buktiKirim.getString(TAG_FOTO),
									thumb_image);
							txtInfo.setText(json.getString(TAG_MESSAGE));
							/*
							btnAddToCart = (ImageButton) findViewById(R.id.btnAddToCart);
							
							btnAddToCart.setOnClickListener(new OnClickListener() {
								
								@Override
								public void onClick(View arg0) {
									// TODO Auto-generated method stub
									//((MyApplication) DetailProductActivity.getApplication()).setStringValue("di menu coy");
									addToBandrekList(id_produk, sNamaProduk, sLinkImage, sHarga);
								}
							});
							*/
						}else {

							txtInfo.setText(json.getString(TAG_MESSAGE));
							
							//ImageView thumb_image = (ImageView) findViewById(R.id.imgBuktiPengiriman);
							//thumb_image.setBackgroundDrawable(R.drawable.butki_pengiriman);
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			});

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog once got all details
			pDialog.dismiss();
		}
	}
	
	
	private void addToBandrekList(String id_produk, String namaProduk, 
			String linkImage, String harga){
		//String msg = ((MyApplication) this.getApplication()).getStringValue();
		Bandrek bandrek = new Bandrek();
		
		bandrek.setId_product(id_produk);
		bandrek.setLink_image(linkImage);
		bandrek.setNama_product(namaProduk);
		bandrek.setHarga(Integer.parseInt(harga));
		bandrek.setQty(2);
		bandrek.setTotalHarga(bandrek.getQty() * Integer.parseInt(harga));
		
		((MyApplication) this.getApplication()).getBandrekList().add(bandrek);
		startActivity(new Intent("com.selly.hanjuang.MenuCart"));
	}
	
}
